package demo.controller;

import demo.pojo.User;
import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserService userService;
    @RequestMapping()
    public String userLogin( HttpServletRequest request)
    {return "login";}
    @RequestMapping("/toLog")
    public String toLog(String username , String password , HttpServletRequest request ,HttpServletResponse response)
    {

        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        User isUser = userService.isUser(user);
        System.out.println(isUser);
        if(isUser!=null)
        {
            request.getSession().setAttribute("user",user);
            Cookie cookie = new Cookie("userId",String.format("%d",user.getId()));
            cookie.setMaxAge(60*30);
            cookie.setPath(request.getContextPath());
            response.addCookie(cookie);
            System.out.println("111111");
        }
        else System.out.println("登录不成功");
        return "forward:/book/all";
    }

}
